<?php
	session_start();
	if(!$_SESSION['login'] == 1 && $_SESSION['sessionid'] !== session_id()) {
		header("Location: ./");
		exit;
	}
	
	if($_SESSION['priv'] == 1) {
		$lang = $_SESSION['lang'];
		require_once("include/lang." . $lang . ".php");
		echo ERR_PRIV;
		exit;
	}
	
	if(isset($_POST['sub'])) {
		header("Location: home.php");
		require_once("include/functions.php");
		$first 		= $_POST['first'];
		$last 		= $_POST['last'];
		$nick 		= $_POST['nick'];
		$addr 		= $_POST['addr'];
		$state	 	= $_POST['state'];
		$zip 		= $_POST['zip'];
		$city		= $_POST['city'];
		$country 	= $_POST['country'];
		// Birthday
		$day		= $_POST['day'];
		$month 		= $_POST['month'];
		$year		= $_POST['year'];
		$notes		= $_POST['notes'];
		$home		= $_POST['home'];
		$work		= $_POST['work'];
		$mobile		= $_POST['mobile'];
		$mail		= $_POST['mail'];
		$msn		= $_POST['msn'];
		$icq		= $_POST['icq'];
		$ym			= $_POST['ym'];
		$user_group = $_SESSION['group'];
		$web		= $_POST['web'];
		
		$contacts->add_contact($first, $last, $nick, $addr, $state, $zip, $city, $country, $day, 
		$month, $year, $notes, $home, $work, $mobile, $mail, $msn, $icq, $ym, $user_group, $web);
		exit;
	}
	
	if(isset($_GET['mode'])) {
		if($_GET['mode'] == "del") {
			if(!isset($_GET['id'])) {
				die("Missing a id!");
			}
			
			else {
				header("Location: home.php");
				require_once("include/functions.php");
				$id = $_GET['id'];
				$contacts->rm_contact($id);
			}
		}
	}
		
	
	include("include/layout.php");

	html_start();
?>

			<br />
			<span class="conin">
				<form action="<?=$_SERVER['PHP_SELF'];?>" method="post">
					<table style="width:600px;">
						<tr>
							<td>
								<fieldset style="width:250px;">
								<legend><?=ADDR_PERSONAL;?></legend>
								<?=ADDR_FIRST;?>
								<br />
								<input type="text" name="first" style="width:230px;" />
								<br />
								<?=ADDR_LAST;?>
								<br />
								<input type="text" name="last" style="width:230px;" />
								<br />
								<?=ADDR_NICK;?>
								<br />
								<input type="text" name="nick" style="width:230px;" />
								<br />
								<?=ADDR_ADDR;?>
								<br />
								<input type="text" name="addr" style="width:230px;" />
								<br />
								<table style="width:250px;">
									<tr>
										<td style="width:124px;"><?=ADDR_STATE;?></td>
										<td><?=ADDR_ZIP;?></td>
									</tr>
									<tr>
										<td style="width:124px;"><input type="text" name="state" style="width:100px;" /></td>
										<td><input type="text" name="zip" style="width:100px;" /></td>
									</tr>
								</table>
								<?=ADDR_CITY;?>
								<br />
								<input type="text" name="city" style="width:230px;" />
								<?=ADDR_COUNTRY;?>
								<select name="country">
								<option value=""> -- <?=ADDR_SEL_COUNT;?> --</option>
								<?php
									$f = fopen(COUNTRIES, "r");
									if(!$f) {
										echo "Error: Could not open COUNTRY file";
										exit;
									}
									$get = fread($f, filesize(COUNTRIES));
									$get = explode("\n", $get);
									
									foreach($get as $country) {
										echo "<option value=\"" . $country . "\">" . $country . "</option>";
									}
									fclose($f);
								?>
								</select>
								<?=ADDR_BIRTH;?>
								<br />
								<select name="day">
									<option value="">---</option>
									<?php
										for($i=1;$i < 32;$i++) {
											echo "<option value=\"" . $i . "\">" . $i . "</option>";
										}
									?>
								</select>
								<select name="month">
									<option value="">---</option>
									<?php
										$mnds = array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
										for($i=1;$i < 13;$i++) {
											echo "<option value=\"" . $i . "\">" . $mnds[$i] . "</option>";
										}
									?>
								</select>
								<select name="year">
									<option value="">----</option>
									<?php
										$y = date("Y");
										for($i=1905;$i < $y;$i++) {
											echo "<option value=\"" . $i . "\">" . $i . "</option>";
										}
									?>
								</select>
								</fieldset>
								<br />
								<fieldset style="width:265px;">
									<legend><?=ADDR_OTHER;?></legend>
									<?=ADDR_NOTES;?><br />
									<textarea name="notes" style="width:235px;height:100px;"></textarea>
								</fieldset>
							</td>
							<td style="vertical-align:top;">
								<fieldset style="width:250px;">
									<legend><?=ADDR_PHONE;?></legend>
									<?=ADDR_HOME;?>
									<br />
									<input type="text" name="home" style="width:230px;" />
									<br />
									<?=ADDR_WORK;?>
									<br />
									<input type="text" name="work" style="width:230px;" />
									<br />
									<?=ADDR_MOBILE;?>
									<br />
									<input type="text" name="mobile" style="width:230px;" />
								</fieldset>
								<br />
								<fieldset style="width:250px;">
									<legend><?=ADDR_ELECTRON;?></legend>
									<?=ADDR_HOMEPAGE;?> (<?=ADDR_EXCL_HTTP;?>)
									<br />
									<input type="text" name="web" style="width:230px;" />
									<br />
									Mail
									<br />
									<input type="text" name="mail" style="width:230px;" />
									<br />
									MSN
									<br />
									<input type="text" name="msn" style="width:230px;" />
									<br />
									ICQ
									<br />
									<input type="text" name="icq" style="width:230px;" />
									<br />
									YM
									<br />
									<input type="text" name="ym" style="width:230px;" />
									
								</fieldset>
							</td>
						</tr>
					</table>
					<br />
					<input type="submit" name="sub" value="<?=ADDR_ADD;?>" />
				</form>
			</span>
<?php
	html_end();
?>